#!/usr/bin/perl
#save_create.cgi

require './diskman-lib.pl';

&ReadParse();

my ($err_msg, $log_ev, $log_msg, $log_pri);

$err_msg="";
$log_ev="";

my $mkfs_lock;
$mkfs_lock = check_lock();
if ($mkfs_lock) {
        `touch /root/mflock`;
	$err_msg = $text{"err_mkfslocked"};
	goto ERROR_OUT;
}

my ($level, $disklist, $volname, $needspare, $sparedisk);

$level = $in{'level'};
$disklist = $in{'listinput'};
$volname = $in{'volname'};
$needspare = $in{'needspare'};
$sparedisk = $in{'sparedisk'};

my (@rdinfo, @dinfo, @free_volumes, @free_disks, $i);

@rdinfo = new_get_check_raid_info();
@dinfo = new_get_diskinfo(\@rdinfo);

@free_volumes = &get_free_volumes(\@rdinfo);
@free_disks = &get_free_disks(\@dinfo);

my ($fv, $found);
$found = 0;
foreach $fv (@free_volumes) {
	if ($fv eq $volname) {
		$found = 1;
		last;
	}
}

if (!$found) {
	$err_msg = $text{"err_configchanged"};
	goto ERROR_OUT;
}

my (@diskname, $dn, $errdisk);
@diskname = split(/\s+/, $disklist);
$errdisk = 0;

my ($sparedev,$index);
if ($needspare =~ /needspare/) {
	if (index(join(' ', @free_disks), $sparedisk) < 0) {
		$sparedev = undef;
		$errdisk = 1;
	}
	else {
		$sparedisk =~ /disk(\d+)/;
		$index=$1-1;
		$sparedev = $dinfo[$index]{'device'}.$hwinfo{'userpart'};
	}
}
else {
	$sparedev = undef;
}

my (@rdevs);
foreach $dn (@diskname) {
	$dn =~ /disk(\d+)/;
	$index=$1-1;

	if (index(join(' ', @free_disks), $dn) < 0) {
		$errdisk = 1;
		last;
	}
	if ($needspare =~ /needspare/) {
		if ($dn ne $sparedisk) {
			push(@rdevs, $dinfo[$index]{'device'}.$hwinfo{'userpart'});
		}
	}
	else {
		push(@rdevs, $dinfo[$index]{'device'}.$hwinfo{'userpart'});
	}
}

if ($errdisk) {
	$err_msg = $text{"err_configchanged"};
	#goto ERROR_OUT;
}

if ($level eq 'linear') {
	if (@rdevs < 1) {
		$err_msg = $text{"err_cgiparam"};
	#	goto ERROR_OUT;
	}
}
elsif ($level == 0) {
	if (@rdevs < 1) {
		$err_msg = $text{"err_cgiparam"};
	#	goto ERROR_OUT;
	}
}
elsif ($level == 1) {
	if (@rdevs < 2) {
		$err_msg = $text{"err_cgiparam"};
        #		goto ERROR_OUT;
	}
}
elsif ($level == 5) {
	if (@rdevs < 3) {
		$err_msg = $text{"err_cgiparam"};
	#	goto ERROR_OUT;
	}
}
elsif ($level == 6){
	$err_msg="raid6:$disklist";
	#goto ERROR_OUT;	
}
else {
	$err_msg = $text{"err_cgiparam"};
	goto ERROR_OUT;
}

# check finished
my ($mddev);
$volname =~ /\S+(\d+)/;

$mddev = get_free_scsi_dev();
#$mddev = $mddev.1;
foreach $d (@dinfo){
  $diskcap = $d->{'capacity'};
  if($diskcap =~ /(\d+)/) {
     $diskcap = $1;
     last;
  } 
}
$err_msg = $mddev;
#goto ERROR_OUT;
my ($rc);
$rc = new_create_volume($level, $disklist, $volname, $mddev, $sparedev, $diskcap);

if ($rc) {
	$log_ev = $text{'log_ev_crtvolume_err'};
	$log_msg= &text('log_msg_crtvolume_err', $volname, 'RAID '.$level);
	nas_syslog($text{'my_log_minor'}, 'naslog_warning', $log_ev, $log_msg);

	$err_msg = $text{'err_crtvol'};
	goto ERROR_OUT;
}

# Create Physical Volume(PV)
`sleep 8`;
`/sbin/lvm pvcreate $mddev`;

$log_ev = $text{'log_ev_crtvolume_ok'};
$log_msg= &text('log_msg_crtvolume_ok', $volname, 'RAID '.$level);
nas_syslog($text{'my_log_minor'}, 'naslog_info', $log_ev, $log_msg);

#begin mkfs. Because mkfs is very time consumed, fork a child process
#get rdinfo again
@rdinfo =  new_get_check_raid_info();
@dinfo = new_get_diskinfo(\@rdinfo);

my ($r, $rr);
$found = 0;
# now $volname should be "volume n"
foreach $r (@rdinfo) {
	if ($r->{'volname'} eq $volname) {
		$rr = $r;
		$found = 1;
		last;
	}
}
if (!$found) {
	$err_msg = $text{"err_configchanged"};
	#`raidstop $mddev`;
	#goto ERROR_OUT;
}
# for 3w, here status should be OK. 
if ($rr->{'status'} ne 'unknown') {
	$err_msg = $text{"err_configchanged"};
	#goto ERROR_OUT;
}

$voldir='/usrvol/'.$volname;
#-- CM: give $voldir right value here!
&redirect("index.cgi");
ERROR_OUT:
&error_setup($text{'crt_err_title'});
&error($err_msg);
#&redirect("");


